<template>
  <div>
    <!-- 用户主页头部导航 -->
    <header class="user-home-header">
      <div class="header-content">
        <div class="left-group">
          <!-- 菜单切换按钮 -->
          <button
            class="menu-toggle"
            :class="{ active: sidebarOpen }"
            @click="sidebarOpen = !sidebarOpen"
            :aria-expanded="sidebarOpen ? 'true' : 'false'"
            aria-label="切换侧边栏"
          >
            <el-icon><Menu /></el-icon>
          </button>
          <div class="logo">
            <router-link to="/user-home" class="logo-link">
              <img class="logo-img" src="/logo_02.png" alt="系统Logo" />
            </router-link>
          </div>
        </div>

        <nav class="nav-menu">
          <router-link to="/user-home" class="nav-item" active-class="active">主页</router-link>
          <router-link to="/features" class="nav-item" active-class="active">功能演示</router-link>
          <router-link to="/about" class="nav-item" active-class="active">关于系统</router-link>
        </nav>

        <div class="user-area">
          <el-dropdown trigger="click" @command="handleUserCommand">
            <div class="user-box" role="button" aria-label="用户菜单">
              <el-avatar
                :size="40"
                :src="avatarUrl"
                shape="circle"
              >
                <template #default>
                  <i class="el-icon-user"></i>
                </template>
              </el-avatar>
              <span class="user-name" :title="displayName">{{ displayName }}</span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </header>

    <!-- 侧边栏遮罩层 -->
    <transition name="fade">
      <div
        v-if="sidebarOpen"
        class="app-sidebar-mask"
        @click="sidebarOpen = false"
      />
    </transition>

    <!-- 抽屉式侧边栏（覆盖在主内容上方） -->
    <aside class="app-sidebar" :class="{ open: sidebarOpen }" @click.stop>
      <nav class="app-sidebar-menu">
        <router-link to="/favorites" class="app-sidebar-item" active-class="active">
          <i class="el-icon-star-on"></i>
          <span>我的收藏</span>
        </router-link>
        <router-link to="/history" class="app-sidebar-item" active-class="active">
          <i class="el-icon-time"></i>
          <span>历史记录</span>
        </router-link>
        <router-link to="/profile" class="app-sidebar-item" active-class="active">
          <i class="el-icon-user"></i>
          <span>个人中心</span>
        </router-link>
        <!-- 添加系统功能导航 -->
        <div class="sidebar-divider"></div>
        <router-link to="/align" class="app-sidebar-item" active-class="active">
          <i class="el-icon-operation"></i>
          <span>文本对齐</span>
        </router-link>
        <router-link to="/preprocess" class="app-sidebar-item" active-class="active">
          <i class="el-icon-set-up"></i>
          <span>数据清洗</span>
        </router-link>
        <router-link to="/retrieve" class="app-sidebar-item" active-class="active">
          <i class="el-icon-search"></i>
          <span>数据检索</span>
        </router-link>
        <router-link to="/llmprocess" class="app-sidebar-item" active-class="active">
          <i class="el-icon-chat-dot-round"></i>
          <span>LLM对话</span>
        </router-link>
      </nav>
    </aside>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Menu } from '@element-plus/icons-vue'

const router = useRouter()
const sidebarOpen = ref(false)

const displayName = computed(() => {
  const cached = JSON.parse(localStorage.getItem('user') || '{}')
  return cached.username || '未登录'
})

const avatarUrl = computed(() => {
  const url = JSON.parse(localStorage.getItem('user') || '{}')?.avatar_url
  return url || ''
})

const handleUserCommand = async (cmd) => {
  switch (cmd) {
    case 'profile':
      router.push('/profile')
      break
    case 'logout':
      await handleLogout()
      break
    default:
      break
  }
}

const handleLogout = async () => {
  try {
    // 清除本地登录状态
    localStorage.removeItem('token')
    localStorage.removeItem('user')
    ElMessage.success('已退出登录')
    router.push('/login')
  } catch (e) {
    // 兜底
    localStorage.removeItem('token')
    localStorage.removeItem('user')
    router.push('/login')
  }
}
</script>

<style scoped>
/* 变量定义 */
:root {
  --primary: #8b5cf6;
  --primary-strong: #7c3aed;
  --primary-light: #ede9fe;
  --text-primary: #1e293b;
  --text-secondary: #475569;
  --bg-page: #f8f5ff;
  --bg-card: #ffffff;
  --border: #e2e8f0;
  --shadow: rgba(149, 157, 165, 0.2);
}

.user-home-header {
  background-color: var(--bg-card);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 2px 12px 0 var(--shadow);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 70px;
  position: relative;
}

.left-group {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* 菜单切换按钮样式 */
.menu-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  background: var(--primary-light);
  color: var(--primary-strong);
  width: 36px;
  height: 36px;
  border-radius: 8px;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.menu-toggle:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.menu-toggle.active {
  background: var(--primary-strong);
  color: #fff;
  border-color: var(--primary-strong);
}

.logo {
  display: flex;
  align-items: center;
}

.logo-link {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.logo-img {
  width: 50px;
  height: 50px;
  object-fit: contain;
}

.nav-menu {
  display: flex;
  gap: 40px;
}

.nav-item {
  padding: 0 15px;
  height: 70px;
  line-height: 70px;
  color: var(--text-secondary);
  text-decoration: none;
  position: relative;
  transition: color 0.3s;
  font-size: 16px;
  font-weight: 500;
}

.nav-item:hover {
  color: var(--primary-strong);
}

.nav-item.active {
  color: var(--primary-strong);
  font-weight: 600;
}

.nav-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 15px;
  right: 15px;
  height: 3px;
  background-color: var(--primary-strong);
  border-radius: 2px;
}

.user-area {
  display: flex;
  align-items: center;
}

.user-box {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: background-color 0.3s;
}

.user-box:hover {
  background-color: var(--primary-light);
}

.user-name {
  font-size: 14px;
  color: var(--text-primary);
  margin-left: 8px;
  max-width: 120px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 侧边栏样式 */
.app-sidebar {
  position: fixed;
  left: 0;
  top: 70px; /* 与头部高度对齐 */
  height: calc(100vh - 70px);
  width: 260px;
  max-width: min(86vw, 320px);
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  box-shadow: 4px 0 20px var(--shadow);
  transform: translateX(-100%);
  transition: transform 0.35s ease;
  z-index: 1500; /* 覆盖主内容 */
  overflow-y: auto;
  border-top-right-radius: 12px;
  border-bottom-right-radius: 12px;
}

.app-sidebar.open {
  transform: translateX(0);
}

.app-sidebar-mask {
  position: fixed;
  left: 0;
  top: 70px;
  width: 100vw;
  height: calc(100vh - 70px);
  background: rgba(15, 23, 42, 0.35);
  z-index: 1400;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.35s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.app-sidebar-menu {
  display: flex;
  flex-direction: column;
  padding: 8px;
}

.sidebar-divider {
  height: 1px;
  background: var(--border);
  margin: 8px 0;
}

.app-sidebar-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-radius: 10px;
  color: var(--text-primary);
  text-decoration: none;
  transition: background-color 0.2s, color 0.2s;
  font-size: 14px;
}

.app-sidebar-item:hover {
  background: var(--primary-light);
  color: var(--primary-strong);
}

.app-sidebar-item.active {
  background: var(--primary-light);
  color: var(--primary-strong);
  border: 1px solid var(--primary-strong);
}

.app-sidebar-item i {
  font-size: 16px;
  width: 16px;
  text-align: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 0 15px;
    height: 60px;
  }

  .logo-img {
    width: 40px;
    height: 40px;
  }

  .nav-menu {
    gap: 20px;
  }

  .nav-item {
    height: 60px;
    line-height: 60px;
    font-size: 14px;
    padding: 0 10px;
  }

  .nav-item.active::after {
    left: 10px;
    right: 10px;
  }

  .user-name {
    display: none;
  }

  .app-sidebar {
    top: 60px;
    height: calc(100vh - 60px);
  }

  .app-sidebar-mask {
    top: 60px;
    height: calc(100vh - 60px);
  }
}

@media (max-width: 480px) {
  .nav-menu {
    gap: 15px;
  }

  .nav-item {
    font-size: 13px;
    padding: 0 8px;
  }
}
</style>